import tkinter as tk
import tkinter.messagebox
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
d={}#name:ip
vd={}#ip:name
#start-ip
def is_valid_ipv4_address(ip):
    try:
        socket.inet_aton(ip)
        return True
    except socket.error:
        return False
def getsip():
    name=e.get()
    if is_valid_ipv4_address(name):
        s.bind((name,8080))
        s.setblocking(0)
        nroot.destroy()
    else:
        tkinter.messagebox.showerror("错误","IP格式错误！")
nroot=tk.Tk()
nroot.title("")
name=""
tk.Label(nroot,text="本机IP地址:").grid(row=0,column=0,rowspan=1,columnspan=3)
e=tk.Entry(nroot)
e.grid(row=1,column=0,rowspan=1,columnspan=2)
b=tk.Button(nroot,text="确定",command=getsip)
b.grid(row=1,column=2,rowspan=1,columnspan=1)
nroot.mainloop()
#end



def wtest(ip):
    try:
        s = socket.create_connection((ip, 80), 2)
        return True
    except Exception as e:
        return False
def fr():
    def nd():
        global vd
        name=e1.get()
        cname=e2.get()
        if is_valid_ipv4_address(name):
            if not wtest(name):
                tkinter.messagebox.showwarning("警告","IP未连通，对方可能不在线！")
            listb.insert("end",cname)
            d[cname]=name
            vd={v: k for k, v in d.items()}
            nroot.destroy()
        else:
            tkinter.messagebox.showerror("错误","IP格式错误！")
    nroot=tk.Tk()
    nroot.title("")
    name=""
    
    tk.Label(nroot,text="IP地址:").grid(row=0,column=0,rowspan=1,columnspan=3)
    e1=tk.Entry(nroot)
    e1.grid(row=1,column=0,rowspan=1,columnspan=3)
    
    tk.Label(nroot,text="计算机名（自定义）:").grid(row=2,column=0,rowspan=1,columnspan=3)
    e2=tk.Entry(nroot)
    e2.grid(row=3,column=0,rowspan=1,columnspan=3)
    
    b=tk.Button(nroot,text="确定",command=nd)
    b.grid(row=4,column=0,rowspan=1,columnspan=3)
    nroot.mainloop()
def reload():
    try:
        while True:
            data, addr = s.recvfrom(65536)
            if addr[0] in vd: listm.insert("end", vd[addr[0]] + ":" + data.decode("utf-8"))
            else: listm.insert("end", addr[0] + ":" + data.decode("utf-8"))      
    except:
        pass
    finally:
        root.after(250, reload)
def sent():
    text=e.get()
    try:
        addr=d[listb.get(listb.curselection())]
    except:
        tkinter.messagebox.showerror("错误","您似乎没选收件人...")
    else:
        s.sendto(text.encode("utf-8"),(addr,8080))
        listm.insert("end","我=>"+vd[addr]+":"+text)
root=tk.Tk()
root.title("Hello Foolish-man聊天器")
listb=tk.Listbox(root,selectmode=tk.SINGLE)
listb.grid(row=0,column=0,rowspan=5,columnspan=2)

buttonins=tk.Button(root,text="添加联系人",command=fr)
buttondel=tk.Button(root,text="删除联系人",command=lambda :listb.delete("active"))
# 移动按钮到第6行，避免与listb重叠
buttonins.grid(row=6,column=0)
buttondel.grid(row=6,column=1)

listm=tk.Listbox(root,width=40)
listm.grid(row=0,column=2,rowspan=6,columnspan=5)

e=tk.Entry(root)
# 移动e到listm不占据的列上，例如第7列
e.grid(row=6,column=2,rowspan=1,columnspan=4)
buttonsnd=tk.Button(root,text="发送",command=sent)
buttonsnd.grid(row=6,column=6)
reload()
root.mainloop()
